package com.exsun.ex1077.ui.activity.login

import android.os.Bundle
import android.os.CountDownTimer
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.blankj.utilcode.util.RegexUtils
import com.exsun.ex1077.R
import com.exsun.ex1077.app.Constant
import com.exsun.ex1077.databinding.ActivityRegisterBinding
import com.exsun.ex1077.extend.toPhotoUrl
import com.exsun.ex1077.network.ApiUrl
import com.exsun.ex1077.ui.activity.web.WebActivity
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.extend.*
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.extend.view.clickNoRepeat
import com.zhangping.fastjetpack.extend.view.isEmpty
import com.zhangping.fastjetpack.extend.view.textString
import kotlinx.android.synthetic.main.activity_register.*

/**
 * 描述：注册
 * 作者：ZhangPing
 * 创建时间：2023/3/15 16:15
 */
class RegisterActivity: BaseActivity<RegisterViewModel, ActivityRegisterBinding>() {

    override fun title() = "注册"

    private lateinit var countDownTimer: CountDownTimer

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        mViewModel.companyMode.set(intent.getIntExtra("pageType", 0) == Constant.PAGE_TYPE_REGISTER_COMPANY)
        countDownTimer = object : CountDownTimer(60000, 1000){
            override fun onTick(millisUntilFinished: Long) {
                val freeTime = millisUntilFinished / 1000
                getCode.text = "${freeTime}秒后重试"
            }

            override fun onFinish() {
                getCode.apply {
                    text = "获取验证码"
                    setBackgroundResource(R.drawable.shape_radius5_bg_theme)
                    isEnabled = true
                }
            }
        }
        getCode.clickNoRepeat {
            if (accountEdit.isEmpty()){
                "请输入账号".showToast()
                return@clickNoRepeat
            }
            val mobile = mobileEdit.textString()
            if (mobile.isEmpty()){
                "请输入手机号".showToast()
                return@clickNoRepeat
            }
            if (!RegexUtils.isMobileSimple(mobile)){
                "手机号码格式不正确".showToast()
                return@clickNoRepeat
            }
            mViewModel.apply {
                checkAccountAndMobile().observe(this@RegisterActivity){
                    sendSms().observe(this@RegisterActivity){
                        getCode.apply {
                            isEnabled = false
                            setBackgroundResource(R.drawable.shape_radius5_bg_9ab9f6)
                        }
                        countDownTimer.start()
                    }
                }
            }
        }
        eye.setOnClickListener {
            mViewModel.apply {
                pwdShow.reversal()
                if (pwdShow.get()) {
                    passwordEdit.transformationMethod = HideReturnsTransformationMethod.getInstance()
                } else {
                    passwordEdit.transformationMethod = PasswordTransformationMethod.getInstance()
                }
                passwordEdit.setSelection(password.get().length)
            }
        }
        agreement.setOnClickListener { mViewModel.agreement.reversal() }
        userAgreement.setOnClickListener {
            toActivity<WebActivity>(Bundle().apply {
                putString("title", "用户协议")
                putString("url", ApiUrl.USER_AGREEMENT)
            })
        }
        privacyAgreement.setOnClickListener {
            toActivity<WebActivity>(Bundle().apply {
                putString("title", "隐私政策")
                putString("url", ApiUrl.PRIVACY_AGREEMENT)
            })
        }
        register.setOnClickListener {
            if (mViewModel.companyMode.get() && companyEdit.textString().isEmpty()){
                "请输入企业名称".showToast()
                return@setOnClickListener
            }
            if (accountEdit.textString().isEmpty()){
                "请输入账号".showToast()
                return@setOnClickListener
            }
            if (mobileEdit.textString().isEmpty()){
                "请输入手机号".showToast()
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileSimple(mobileEdit.textString())){
                "手机号码格式不正确".showToast()
                return@setOnClickListener
            }
            if (codeEdit.textString().isEmpty()){
                "请输入验证码".showToast()
                return@setOnClickListener
            }
            if (passwordEdit.textString().isEmpty()){
                "请输入密码".showToast()
                return@setOnClickListener
            }
            if (!mViewModel.agreement.get()){
                "请阅读并同意《用户协议》和《隐私条款》".showToast()
                return@setOnClickListener
            }
            mViewModel.register()
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.registerResult.observe(this){
            parseState(it, {
                it?.let {
                    "token".putCache(it.token)
                    "account".putCache(it.username)
                    "name".putCache(it.name)
                    "companyName".putCache(it.companyName)
                    "photo".putCache(it.photo.toPhotoUrl())
                    finishToActivity<RegisterSuccessActivity>()
                }
            })
        }
    }

    override fun onDestroy() {
        countDownTimer.cancel()
        super.onDestroy()
    }
}